package com.woniuxy.ticketorder;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableFeignClients
@EnableCircuitBreaker   //启用熔断 yml也有配置
@SpringBootApplication
//@EnableHystrixDashboard //开启熔断器的监控功能
public class TicketOrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(TicketOrderApplication.class, args);
    }

    @LoadBalanced //开启负载均衡的功能,实现服务调用
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    //负载均衡策略
    public IRule rule(){
        //随机策略
        return new RandomRule();
    }

    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
